package com.example.room;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;


import android.content.Context;

import androidx.room.Room;

import com.blankj.utilcode.util.SDCardUtils;
import com.example.room.dao.OrderDao;
import com.example.room.dao.UserDao;
import com.example.room.entity.Order;
import com.example.room.entity.User;

import java.io.File;

@Database(entities = {User.class, Order.class}, version = 1, exportSchema=false)
//@Database(entities = {User.class, Order.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {

    public abstract UserDao userDao();
    public abstract OrderDao orderDao();





    //========================
    public static String dbPath = SDCardUtils.getSDCardPathByEnvironment()
            + File.separator + "ods"
            + File.separator + "test"
            + File.separator + "Test1_0.db";

    String dbPath2 = SDCardUtils.getSDCardPathByEnvironment()
            + File.separator + "ods"
            + File.separator + "test"
            + File.separator + "Test2_0.db";


    public AppDatabase build(Context context, String dbPath) {
        AppDatabase appDatabase = Room.databaseBuilder(context, AppDatabase.class, dbPath)
                .build();

        return appDatabase;
    }

    public AppDatabase update(Context context, String dbPath) {
        Migration MIGRATION_1_2 = new Migration(1, 2) {
            @Override
            public void migrate(@NonNull SupportSQLiteDatabase database) {
                // 在这里执行数据库升级的 SQL 语句
                // 示例：向 users 表添加一个 gender 字段
                database.execSQL("ALTER TABLE users ADD COLUMN gender TEXT");
                database.execSQL("ALTER TABLE users ADD COLUMN user_name_pinyin TEXT");

                // 示例：增加一个新表
                //database.execSQL("CREATE TABLE IF NOT EXISTS new_table (id INTEGER PRIMARY KEY)");

            }
        };

        AppDatabase appDatabase =  Room.databaseBuilder(context, AppDatabase.class, dbPath)
                .addMigrations(MIGRATION_1_2)
                .build();

        return appDatabase;
    }
    //========================


}

